package org.acghub.common;

public class Resp<T>
{

    public static final int SUCCESS = 0;

    public static final int FAILED = -1;

    private int code = SUCCESS;

    private Object reason;

    private T data;

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public Object getReason()
    {
        return reason;
    }

    public void setReason(Object reason)
    {
        this.reason = reason;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }

    public boolean isSuccess()
    {
        return code == SUCCESS;
    }

    public boolean isFailed()
    {
        return code != SUCCESS;
    }

    public boolean reasonIsString()
    {
        return reason instanceof String;
    }

    public boolean reasonIsThrowable()
    {
        return reason instanceof Throwable;
    }

    public String getReasonAsString()
    {
        return ObjectUtils.defaultIfNull(reasonIsString() ? (String) reason : null, "");
    }

    public Throwable getReasonAsThrowable()
    {
        return reasonIsThrowable() ? (Throwable) reason : null;
    }

}
